package com.ruoyi.parttime.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.parttime.common.Statistics;
import com.ruoyi.parttime.domain.DataStatistics;
import com.ruoyi.parttime.service.IApplyForParrtimeService;
import com.ruoyi.parttime.service.IParttimePublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/home/page")
public class HomePage {
    @Autowired
    IParttimePublishService parttimePublishService;
    @Autowired
    IApplyForParrtimeService applyForParrtimeService;
    @Autowired
    RedisCache redisCache;

    /**
     * 获取今日兼职发布，申请，成功，失败数量
     */
    @GetMapping("/parttime/num")
    @Anonymous
    public AjaxResult getToDayNum() {
        String key = Constants.HOME_PAGE_DATA;
        //根据键判断数据是否存在
        if (redisCache.hasKey(key)) {
            //如果存在，直接从缓存中拿取，再返回,
            List<DataStatistics> redisList = redisCache.getCacheObject(key);
            return AjaxResult.success(redisList);
        }
        //不存在，先从主数据库中获取数据,然后再存入缓存中
        Integer parttimePublishNum = parttimePublishService.getStatisticsNum();
        Integer applyforNum = applyForParrtimeService.getStatisticsNum();
        Map<Integer, Integer> map = applyForParrtimeService.getStatisticsNumBystatus();
        List<DataStatistics> list = new ArrayList<>();
        DataStatistics statistics1 = DataStatistics.getInstance();
        DataStatistics statistics2 = DataStatistics.getInstance();
        DataStatistics statistics3 = DataStatistics.getInstance();
        DataStatistics statistics4 = DataStatistics.getInstance();
        //今日兼职发布数
        statistics1.setName(Statistics.TO_DAY_PART_TIME_PUBLISH_NUM);
        statistics1.setValue(parttimePublishNum);
        list.add(statistics1);
        //今日兼职申请数
        statistics2.setName(Statistics.TO_DAY_APPLY_FOR_NUM);
        statistics2.setValue(applyforNum);
        list.add(statistics2);
        //今日申请成功数
        statistics3.setName(Statistics.TO_DAY_APPLY_FOR_SUCCEED_NUM);
        int a = map.get(1) == null ? 0 : map.get(1);
        statistics3.setValue(a);
        list.add(statistics3);
        //今日申请失败数量
        statistics4.setName(Statistics.TO_DAY_APPLY_FOR_LOSE_NUM);
        a = map.get(2) == null ? 0 : map.get(2);
        statistics4.setValue(a);
        list.add(statistics4);
        //存入缓存
        redisCache.setCacheObject(key, list);
        //设置存活时间为20分钟
        redisCache.expire(key, 20 * 60);
        return AjaxResult.success(list);
    }
    /**
     * 获取本年兼职失败和成功数量
     */
    @GetMapping("/parttime/month")
    @Anonymous
    public AjaxResult getMonthNum(){
        return AjaxResult.success(applyForParrtimeService.getMonthNum());
    }
}
